﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FunctionPoint.Common;
using FunctionPoint.Utility;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TextManager.Interop;
using FunctionPoint.UIManagers;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Editor;
using FunctionPoint.Services;

namespace FunctionPoint.UserControls
{
    /// <summary>
    /// Interaction logic for FunctionPointSelectorTreeView.xaml
    /// </summary>
    public partial class FunctionPointSelectorTreeControl : UserControl
    {

        private IVsEditorAdaptersFactoryService _editorFactory = null;
        public IVsEditorAdaptersFactoryService EditorFactory
        {
            get
            {
                return _editorFactory;
            }
            set
            {
                _editorFactory = value;
            }
        }

        private IFunctionPointManagerService _managementService = null;
        private IFunctionPointManagerService ManagementService
        {
            get
            {
                if (_managementService == null)
                    _managementService = ServiceProvider.GlobalProvider.GetService(typeof(SFunctionPointManagerService)) as IFunctionPointManagerService;

                return _managementService;
            }
        }

        public FunctionPointSelectorTreeControl()
        {
            InitializeComponent();
        }

        public void RepaintAndReQuery()
        {
            var selectedItem = ((ComboBox)cmbSelectedQuery).SelectedItem;

            if (selectedItem is WorkItemQuery)
                ManagementService.GetQueryWorkItems((WorkItemQuery)selectedItem);

        }

        private void cmbSelectedQuery_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedItem = ((ComboBox)e.OriginalSource).SelectedItem;

            if (selectedItem is WorkItemQuery)
            {
                WorkItemQuery wiQuery = (WorkItemQuery)selectedItem;

                if (!wiQuery.IsLoaded || wiQuery.IsNeedingRefresh)
                    ManagementService.GetQueryWorkItems(wiQuery);

            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox workItemCheckbox = sender as CheckBox;
            if (workItemCheckbox != null)
            {
                if (ManagementService != null)
                {
                    Common.WorkItem workItem = workItemCheckbox.DataContext as WorkItem;

                    if (workItem != null)
                    {
                        // at this point the databinding should have already set IsselctedTo true, which would still have reference
                        // to the one in the service.  

                        
                        if (workItemCheckbox.IsChecked.GetValueOrDefault(false))
                            ManagementService.AddWorkItemToDisplay(workItem);
                        else
                            ManagementService.RemoveWorkItemToDisplay(workItem);
                    }
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
